{template 'tpl_header'}
<div class="weui-panel weui-panel_access">
    <div class="weui-panel weui-panel_access weui-cell_access" style="margin-bottom:0;">
        <div class="weui-panel__bd">
            <a href="{php echo url('entry', array('m' => 'vj_bontrip','do' => 'insurance','op' => 'product','id' => $id),true);}" class="weui-cell_link" style="color:#333333;">
                <div class="weui-media-box weui-media-box_text">
                    <div class="weui-media-box__title">保障权益及产品解读</div>
                    <div class="weui-media-box__desc insurance-payment_desc">
                        {if !empty($security) }
                        {loop $security $s}
                        <div class="insurance-payment_info font_color_2">
                            <div class="insurance-payment_title">
                                {$s['security_title']}
                            </div>
                            <div class="insurance-payment_price">
                                {php echo number_format($s['security_price'],2)}元
                            </div>
                            <div class="clear"></div>
                        </div>
                        {/loop}
                        {/if}
                        <div class="insurance-payment_desc__ft">
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="weui-panel__ft">
        </div>
    </div>
</div>

<div class="weui-panel weui-panel_access">
    <div class="weui-cells" style="margin-top: 0;font-size: 14px;">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>被保人</p>
            </div>
            <div class="weui-cell__ft">

            </div>
        </div>
        {if $adult_number != 0 }
        {loop $adult $ad}
        <div class="weui-cell weui-cell_access" onclick="insured({$ad},'adult');">
            <div class="weui-cell__hd"><label class="weui-label">成年人{$ad}</label></div>
            <div class="weui-cell__bd">
                {$insured_info['adult'][$ad]['real_name']}
            </div>
            <div class="weui-cell__ft">
            </div>
        </div>
        {/loop}
        {/if}
        {if $child_number != 0 }
        {loop $child $ch}
        <div class="weui-cell weui-cell_access" onclick="insured({$ch},'child');">
            <div class="weui-cell__hd"><label class="weui-label">未成年人{$ch}</label></div>
            <div class="weui-cell__bd">
                {$insured_info['child'][$ch]['real_name']}
            </div>
            <div class="weui-cell__ft">
            </div>
        </div>
        {/loop}
        {/if}
    </div>
</div>

<div class="weui-panel weui-panel_access">
    <div class="weui-cells" style="margin-top: 0;font-size: 14px;">
        <div class="weui-cell">
            <div class="weui-cell__hd font_color_2"><label class="weui-label">投保人</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" type="text" name="contact_name" placeholder="请填写投保人"
                       value="{$member['real_name']}">
            </div>
            <div class="weui-cell__ft">
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd font_color_2"><label class="weui-label">手机号码</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" type="tel" name="contact_mobile" placeholder="请填写手机号"
                       value="{$member['mobile']}">
            </div>
            <div class="weui-cell__ft">
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd font_color_2"><label class="weui-label">邮箱</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" type="email" name="contact_email" placeholder="请填写邮箱"
                       value="{$member['email']}">
            </div>
            <div class="weui-cell__ft">
            </div>
        </div>
    </div>
</div>
<div class="weui-footer">
    <p class="weui-footer__links">
        <a href="javascript:void(0);" class="weui-footer__link open-popup" data-target="#half">查看详细</a>
    </p>
</div>
<div id="half" class='weui-popup__container popup-bottom'>
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
        <div class="toolbar">
            <div class="toolbar-inner" style="background-color: #11a4c0;">
                <div class="pay-title">
                    <p>费用详细</p>
                </div>
                <div class="pay-price"><span class="pay-price_left">共</span><span class="pay-price_right">￥{$insurance['total_amount']}({$day}天)</span>
                </div>
            </div>
        </div>
        <div class="modal-content">
            <div class="weui-grids" style="padding: 15px .5rem 30px .5rem">
                {if $adult_number != 0 }
                <div class="pay-price_content">
                    <div class="pay-price_content_title">
                        成人
                    </div>
                    <div class="pay-price_content_desc">
                        {$adult_number} x {php echo number_format($insurance['adult_price'],2)}
                    </div>
                </div>
                <div class="clear"></div>
                {/if}
                {if $child_number != 0 }
                <div class="pay-price_content">
                    <div class="pay-price_content_title">
                        儿童
                    </div>
                    <div class="pay-price_content_desc">
                        {$child_number} x {php echo number_format($insurance['child_price'],2)}
                    </div>
                </div>
                <div class="clear"></div>
                {/if}
            </div>
        </div>
    </div>
</div>
<label for="weuiAgree" class="weui-agree">
    <input id="weuiAgree" type="checkbox" class="weui-agree__checkbox">
    <span class="weui-agree__text">
        我已阅读并同意保险条款、须知及免除责任。
      </span>
</label>

<div class="nav-seize-seat"></div>
<div class="nav-price">
    <div class="nav-price_info">
        <span class="nav-price_info_title">总额：</span><span
            class="nav-price_info_price">￥{$insurance['total_amount']}</span>
    </div>
    <div class="nav-price_button">
        <button id="sub" class="weui-btn weui-btn_mini weui-btn_primary" style="margin-top: 0">提交订单</button>
    </div>
    <div class="clear"></div>
</div>
{template 'tpl_footer'}
<script>
    var is_look = false;
    var adult_number = '{$adult_number}';
    var child_number = '{$child_number}';
    var is_insured = '{$is_insured}';

    function insured(index, type) {
        var url = "{php echo url('entry', array('m' => 'vj_bontrip','do' => 'insured','insurance_id' => $id,'start_day' => $start_day,'finish_day' => $finish_day),true);}";
        url += "&adult_number=" + adult_number + "&child_number=" + child_number + "&index=" + index + "&type=" + type;
        window.location.href = url;
    }

    $(function () {
        $('#sub').click(function () {
            //防止重复点击
            if (!is_look) {
                is_look = true;
                var contact_name = $('input[name="contact_name"]').val();
                var contact_mobile = $('input[name="contact_mobile"]').val();
                var contact_email = $('input[name="contact_email"]').val();
                if (is_insured != '1') {
                    is_look = false;
                    $.toast('请选择被保人', "cancel");
                    return false;
                }
                if (contact_name == '') {
                    is_look = false;
                    $.toast('请填写投保人', "cancel");
                    return false;
                }

                if (contact_mobile == '') {
                    is_look = false;
                    $.toast('请填写投保人手机', "cancel");
                    return false;
                }

                if (contact_email == '') {
                    is_look = false;
                    $.toast('请填写投保人邮箱', "cancel");
                    return false;
                }

                if (!$('#weuiAgree').is(':checked')) {
                    is_look = false;
                    $.toast('请阅读并同意保险条款', "cancel");
                    return false;
                }

                var myreg = /^[1][3-8][0-9]{9}$/;
                if (!myreg.test(contact_mobile)) {
                    is_look = false;
                    $.toast('请填写有效的手机号码', "cancel");
                    return false;
                }
                var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if (!reg.test(contact_email)) {
                    is_look = false;
                    $.toast('请填写有效的邮箱', "cancel");
                    return false;
                }

                //异步生成订单
                var data = {
                    'action': 'add_insurance_order',
                    'insurance_id': '{$id}',
                    'start_day': '{$start_day}',
                    'finish_day': '{$finish_day}',
                    'adult_number' : '{$adult_number}',
                    'child_number' : '{$child_number}',
                    'contact_name' : contact_name,
                    'contact_mobile' : contact_mobile,
                    'contact_email' : contact_email,
                };
                $.ajax({
                    url: "{php echo $this->createMobileUrl('ajax')}",
                    type: 'post',
                    data: data,
                    dataType: 'json',
                    success: function (res) {
                        if (res.status == 1) {
                            url = "{php echo url('entry', array('m' => 'vj_bontrip','do' => 'insurance','op' => 'payment'),true); }";
                            url += "&id=" + res.data;
                            window.location.href = url;
                        } else {
                            is_look = false;
                            $.toast(res.msg, "cancel");
                        }

                    },
                    error: function (res) {
                        $.toast('请求失败，请检测网络或联系客服', "cancel");
                    }
                });
            }
        });
    });
</script>